#include "mymain.h"
#include <QThreadPool>
#include "printtask.h"
#include "mydialog.h"
#include <QEventLoop>
#include <QDebug>

MyMain::MyMain(QWidget *parent) : QWidget(parent)
{
//    char *lpc = new char[15];
    setAttribute(Qt::WA_DeleteOnClose);
    pool = new QThreadPool(this);

}
MyMain::~MyMain()
{
    pool->waitForDone();
}

void MyMain::Do()
{
    //设置最大线程数为3的一个线程池

    pool->setMaxThreadCount(5);

    for(int i = 0; i < 10; i++)
    {
        PrintTask *pt = new PrintTask(this);
        connect(pt, SIGNAL(dialog_show(void*)), this, SLOT(dialog_show(void*)));
        pool->start(pt);
        qDebug() << "---------------";
    }

//    pool->waitForDone();
    qDebug() << "haha";
    QEventLoop e;
    e.exec();
}

void MyMain::dialog_show(void* p)
{
    qDebug() << "in dialog_show slot, current thread id is " << QThread::currentThread() << " , from thread id is" << p;
    MyDialog *d = new MyDialog(this);
    d->setFromThreadId(p);
    connect(d, SIGNAL(accepted()), this, SLOT(dialog_accept()));
    connect(d, SIGNAL(transThreadid(void*)), this, SLOT(receiveThreadid(void*)));
//    MyDialog d(this);
//    QEventLoop e;
//    connect(d, &MyDialog::finished, &e, [&e](int i) {
//        e.quit();
//    });
    d->show();
//    e.exec();
}

void MyMain::dialog_accept()
{
    qDebug() << "dialog_accept, current tid is " << QThread::currentThread();
}

void MyMain::receiveThreadid(void *p)
{
    qDebug() << "receiveThreadid is " << p;
}
